<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nyext="urn:noury-extensions" version="1.0">

	<msxsl:script language="C#" implements-prefix="nyext">
    <msxsl:assembly name="ICSharpCode.NRefactory" />
    <msxsl:assembly name="System.Design" />
		<![CDATA[
      public static string xmlEncode(string s) {
        return s;
      }

      public static string stringFormat(string format, string arg0) {
        return string.Format(format, arg0);
      }

      public static string stringFormat(string format, string arg0, string arg1) {
        return string.Format(format, arg0, arg1);
      }

      public static string getAssemblyName(string assembly, string path) {
        try
        {
          byte[] buffer=null;
          string ap=System.IO.Path.Combine(path, assembly+".dll");

          using (System.IO.FileStream fs=new System.IO.FileStream(ap, System.IO.FileMode.Open))
          {
            buffer=new byte[(int)fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
          }

          return AppDomain.CurrentDomain.Load(buffer).FullName;
        } catch
        {
          return assembly;
        }
      }
      
      public static void generateXmlFile(XPathNodeIterator src, string path) {
        using (XmlTextWriter xtw=new XmlTextWriter(path, null))
          while (src.MoveNext())
            xtw.WriteNode(src.Current, false);
      }

      public static void generateFile(string src, string path)
      {
/*        using (System.IO.StringReader sr=new System.IO.StringReader(src))
        {
          using (ICSharpCode.NRefactory.IParser parser=ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, sr))
          {
            parser.Parse();
            System.Collections.Generic.List<ICSharpCode.NRefactory.ISpecial> specials=parser.Lexer.SpecialTracker.RetrieveSpecials();

            ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor visitor=new ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor();
            visitor.Options.IndentationChar=' ';

            using (ICSharpCode.NRefactory.PrettyPrinter.SpecialNodesInserter.Install(specials, visitor))
            {
              parser.CompilationUnit.AcceptVisitor(visitor, null);
  
              using (System.IO.StreamWriter sw=new System.IO.StreamWriter(path, false))
              {
                sw.Write(visitor.Text);
              }
            }
          }
        }
*/
        using (System.IO.StreamWriter sw=new System.IO.StreamWriter(path, false))
        {
          sw.Write(src);
        }

      }

      public static void generateTextFile(string src, string path)
      {
        using (System.IO.StreamWriter sw=new System.IO.StreamWriter(path, false))
        {
          sw.Write(src);
        }
      }

      public static void copyFile(string src, string dest)
      {
          System.IO.File.Copy(src, dest, true);
      }

      public static void generateResource(XPathNodeIterator src, string name, string nspace, string path)
      {
        using (System.CodeDom.Compiler.TempFileCollection tfc=new System.CodeDom.Compiler.TempFileCollection())
        {
          string srcFile=tfc.AddExtension("resx", false);
          generateXmlFile(src, srcFile);

          string[] errors;
          System.CodeDom.Compiler.CodeDomProvider provider=new Microsoft.CSharp.CSharpCodeProvider();

          using (System.IO.StreamWriter sw=new System.IO.StreamWriter(path, false))
          {
            provider.GenerateCodeFromCompileUnit(
              System.Resources.Tools.StronglyTypedResourceBuilder.Create(srcFile, name, nspace, nspace, provider, false, out errors),
              sw,
              new System.CodeDom.Compiler.CodeGeneratorOptions()
            );
          }
        }
      }

      public static string combinePath(string path1, string path2) {
        return System.IO.Path.Combine(path1, path2);
      }

			public static string getFileName(string name) {
				return name;
			}

      public static bool isValueType(string name)
      {
          return Type.GetType(name).IsValueType;
      }

			public static string getModelFileName(string name) {
				return name+".Model";
			}

			public static string getDesignerFileName(string name) {
				return name+".Designer";
			}

			public static string getFullPath(string path) {
				return System.IO.Path.GetFullPath(path);
			}

      public static string getNewGuid() {
        return Guid.NewGuid().ToString("D").ToUpper();
      }

      public static string stringReplace(string instance, string toBeReplaced, string replacement) {
        return instance.Replace(toBeReplaced, replacement);
      }

      public static string getShortPathName(string path) {
        System.Text.StringBuilder shortNameBuffer=new System.Text.StringBuilder(256);
        int bufferSize=shortNameBuffer.Capacity;

        int result=GetShortPathName(path, shortNameBuffer, bufferSize);

        return shortNameBuffer.ToString();
      }

      public static string toLower(string s) {
        return s.ToLower();
      }

      [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
      static extern int GetShortPathName(
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
        string lpszLongPath,
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
        StringBuilder lpszShortPath,
        int cchBuffer);
		]]>
	</msxsl:script>

  <xsl:template match="*" mode="path-to-root">
    <xsl:param name="root" />
    <xsl:param name="separator" select="'\'" />
    <xsl:for-each select="ancestor::directory[.=$root/descendant::directory]">
      <xsl:value-of select="concat('..', $separator)" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*" mode="path-from-root">
    <xsl:param name="root" />
    <xsl:param name="separator" select="'\'" />
    <xsl:for-each select="ancestor::directory[.=$root/descendant::directory]">
      <xsl:value-of select="concat(@name, $separator)" />
    </xsl:for-each>
  </xsl:template>

	<xsl:output indent="yes" encoding="UTF-8" />

</xsl:stylesheet>
